<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Request;

class Goodsrule extends Base{


   public function goods(){
       return $this->belongsTo('Goods');
   }


    public function getRulePriceAttr($value,$data)
    {
      $request = Request::instance();

      //后台价格变化

      if($request->module() == 'admin'){
          //不是编辑的时候，默认为 价格2
          if($request->action() != 'edit'){

              //价格体现
                if(input('modelid') > 3){
                  $priceType = model('Goodsmodel')->where(['id'=>input('modelid')])->value('price_type');
                switch ($priceType) {
                  case 1:
                    return $data['rule_price'];
                    break;
                  case 2:
                    return $data['rule_price2'];
                    break;
                  case 3:
                    return $data['rule_price3'];
                    break;
                  case 4:
                    return $data['rule_price4'];
                    break;
                  default:
                    return $data['rule_price2'];
                    break;
                }

                }else{
                    return $data['rule_price2'];
                }

          }else{
              return $value;
          }
      }else{
        //前端

        if(cookie('goodsmodel_id')){
            $priceType = model('Goodsmodel')->where(['id'=>cookie('goodsmodel_id')])->value('price_type');
            switch ($priceType) {
              case 1:
                return $data['rule_price'];
                break;
              case 2:
                return $data['rule_price2'];
                break;
              case 3:
                return $data['rule_price3'];
                break;
              case 4:
                return $data['rule_price4'];
                break;
              default:
                return $data['rule_price2'];
                break;
            }
        }else{
            return $data['rule_price2'];
        }



      }



    }


}
